package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.PageData;
import com.woniu.secondhand.constant.SecondhandMessageConstant;
import com.woniu.secondhand.constant.SecondhandOrderConstant;
import com.woniu.secondhand.constant.SecondhandProductConstant;
import com.woniu.secondhand.entity.dto.*;
import com.woniu.secondhand.entity.vo.SellProductSearchVO;
import com.woniu.secondhand.entity.vo.WantProductSearchVO;
import com.woniu.secondhand.mapper.SecondhandOrderMapper;
import com.woniu.secondhand.mapper.SecondhandProductMapper;
import com.woniu.secondhand.pojo.SecondhandOrder;
import com.woniu.secondhand.pojo.SecondhandProduct;
import com.woniu.secondhand.service.ISecondhandMessageService;
import com.woniu.secondhand.service.ISecondhandProductService;
import com.woniu.user.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@Service
@Slf4j
public class SecondhandProductServiceImpl extends ServiceImpl<SecondhandProductMapper, SecondhandProduct> implements ISecondhandProductService {

    @Autowired
    private SecondhandProductMapper productMapper;
    @Autowired
    private SecondhandOrderMapper orderMapper;
    @Autowired
    private ISecondhandMessageService messageService;

    @Override
    public PageData<List<SellProductSearchVO>> searchSell(Integer currentPage, Integer pageSize, SellProductSearchDTO sellProductSearchDTO) {
        IPage page = new Page(currentPage, pageSize);
        IPage<SellProductSearchVO> pageList = productMapper.searchSell(page, sellProductSearchDTO);
        PageData pageData = new PageData<>();
        pageData.setTotal(pageList.getTotal());
        pageData.setData(pageList.getRecords());
        return pageData;
    }

    @Override
    public PageData<List<WantProductSearchVO>> searchWant(Integer currentPage, Integer pageSize, WantProductSearchDTO wantProductSearchDTO) {
        IPage page = new Page(currentPage, pageSize);
        IPage<WantProductSearchVO> pageList = productMapper.searchWant(page, wantProductSearchDTO);
        PageData pageData = new PageData<>();
        pageData.setTotal(pageList.getTotal());
        pageData.setData(pageList.getRecords());
        return pageData;
    }

    @Override
    public PageData<List<SecondhandProduct>> searchSellAndUnverified(Integer currentPage, Integer pageSize, SellProductSearchDTO sellProductSearchDTO) {
        IPage page = new Page(currentPage, pageSize);
        IPage<SecondhandProduct> pageList = productMapper.searchSellAndUnverified(page, sellProductSearchDTO);
        PageData pageData = new PageData<>();
        pageData.setTotal(pageList.getTotal());
        pageData.setData(pageList.getRecords());
        return pageData;
    }

    @Override
    public PageData<List<SecondhandProduct>> searchWantAndUnverified(Integer currentPage, Integer pageSize, WantProductSearchDTO wantProductSearchDTO) {
        IPage page = new Page(currentPage, pageSize);
        IPage<SecondhandProduct> pageList = productMapper.searchWantAndUnverified(page, wantProductSearchDTO);
        PageData pageData = new PageData<>();
        pageData.setTotal(pageList.getTotal());
        pageData.setData(pageList.getRecords());
        return pageData;
    }

    @Override
    public int updateToVerify(Integer productId) {
        SecondhandProduct product = productMapper.selectById(productId);
        product.setVerifyStatus(SecondhandProductConstant.VERIFIED);
        int row = productMapper.updateById(product);
        return row;
    }

    @Override
    public SecondhandProduct addSellProduct(SellProductAddDTO productAddDTO, UserLoginVO userLoginVO) {
        SecondhandProduct product = new SecondhandProduct();
        product.setTitle(productAddDTO.getTitle());
        product.setContent(productAddDTO.getContent());
        product.setMobile(productAddDTO.getMobile());
        product.setPrice(productAddDTO.getPrice());
        product.setProducerId(userLoginVO.getId());
        product.setType(SecondhandProductConstant.SELL);
        product.setCategoryId(productAddDTO.getCategoryId());
        product.setDeliveryStatus(productAddDTO.getDeliveryStatus());
        product.setDeleteStatus(SecondhandProductConstant.UNDELETED);
        product.setBuyStatus(SecondhandProductConstant.UNSOLD);
        product.setVerifyStatus(SecondhandProductConstant.UNVERIFIED);
        product.setPic(productAddDTO.getPic());
        productMapper.insert(product);
        return product;
    }

    @Override
    public SecondhandProduct addWantProduct(WantProductAddDTO productAddDTO, UserLoginVO userLoginVO) {
        SecondhandProduct product = new SecondhandProduct();
        product.setTitle(productAddDTO.getTitle());
        product.setContent(productAddDTO.getContent());
        product.setMobile(productAddDTO.getMobile());
        product.setPrice(productAddDTO.getPrice());
        product.setProducerId(userLoginVO.getId());
        product.setType(SecondhandProductConstant.WANT);
        product.setCategoryId(productAddDTO.getCategoryId());
        product.setDeliveryStatus(SecondhandProductConstant.FREE_SHIPPING);
        product.setDeleteStatus(SecondhandProductConstant.UNDELETED);
        product.setBuyStatus(SecondhandProductConstant.UNSOLD);
        product.setVerifyStatus(SecondhandProductConstant.UNVERIFIED);
        product.setPic(productAddDTO.getPic());
        productMapper.insert(product);
        return product;
    }

    @Override
    public String contactToProducer(Integer productId, UserLoginVO userLoginVO) {
        SecondhandProduct product = productMapper.selectById(productId);
        String mobile = product.getMobile();
        MessageAddDTO messageAddDTO = new MessageAddDTO();
        messageAddDTO.setProductId(productId);
        messageAddDTO.setMessageType(SecondhandMessageConstant.CONTACT_SUCCESS_MESSAGE);
        messageService.addMessage(messageAddDTO, userLoginVO);
        return mobile;
    }

    @Override
    public int updateToUnsold(Integer productId) {
        SecondhandProduct product = productMapper.selectById(productId);
        product.setBuyStatus(SecondhandProductConstant.UNSOLD);
        int row = productMapper.updateById(product);
        return row;
    }

    @Override
    public List<SecondhandProduct> listPublish(UserLoginVO userLoginVO) {
        LambdaQueryWrapper<SecondhandProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandProduct::getProducerId, userLoginVO.getId());
        wrapper.eq(SecondhandProduct::getBuyStatus, SecondhandProductConstant.UNSOLD);
        List<SecondhandProduct> productList = productMapper.selectList(wrapper);
        return productList;
    }

    @Override
    public List<SecondhandProduct> listSold(UserLoginVO userLoginVO) {
        LambdaQueryWrapper<SecondhandProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandProduct::getProducerId, userLoginVO.getId());
        wrapper.eq(SecondhandProduct::getBuyStatus, SecondhandProductConstant.SOLD);
        List<SecondhandProduct> productList = productMapper.selectList(wrapper);
        return productList;
    }

    @Override
    public List<SecondhandProduct> listBought(UserLoginVO userLoginVO) {
        LambdaQueryWrapper<SecondhandOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandOrder::getConsumerId, userLoginVO.getId());
        wrapper.eq(SecondhandOrder::getStatus, SecondhandOrderConstant.RECEIVE_SUCCESS).or().eq(SecondhandOrder::getStatus, SecondhandOrderConstant.COMMENT_SUCCESS);
        List<SecondhandOrder> orderList = orderMapper.selectList(wrapper);
        List<SecondhandProduct> productList = new ArrayList<>();
        for (SecondhandOrder order : orderList) {
            SecondhandProduct product = productMapper.selectById(order.getProductId());
            productList.add(product);
        }
        return productList;
    }

    @Override
    public BigDecimal getEarned(UserLoginVO userLoginVO) {
        LambdaQueryWrapper<SecondhandProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandProduct::getProducerId, userLoginVO.getId());
        wrapper.eq(SecondhandProduct::getBuyStatus, SecondhandProductConstant.SOLD);
        List<SecondhandProduct> productList = productMapper.selectList(wrapper);
        BigDecimal earned = BigDecimal.valueOf(0);
        for (SecondhandProduct product : productList) {
            earned = earned.add(product.getPrice());
        }
        return earned;
    }
}
